package com.androsz.electricsleepbeta.app;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androsz.electricsleepbeta.R;
import com.androsz.electricsleepbeta.alarmclock.AlarmClock;
import com.androsz.electricsleepbeta.content.StartSleepReceiver;
import com.androsz.electricsleepbeta.db.SleepContentProvider;
import com.androsz.electricsleepbeta.db.SleepHistoryDatabase;
import com.androsz.electricsleepbeta.db.SleepRecord;
import com.androsz.electricsleepbeta.util.DeviceUtil;
import com.androsz.electricsleepbeta.widget.SleepChart;
import com.androsz.electricsleepbeta.widget.calendar.MonthActivity;

/**
 * Front-door {@link Activity} that displays high-level features the application
 * offers to users.
 */
public class HomeActivity extends CustomTitlebarActivity {

	private class LoadLastSleepChartTask extends
			AsyncTask<String, Void, Cursor> {

		@Override
		protected Cursor doInBackground(String... params) {
			return managedQuery(SleepContentProvider.CONTENT_URI, null, null,
					new String[] { params[0] }, SleepRecord.KEY_TITLE);
		}

		@Override
		protected void onPostExecute(final Cursor cursor) {
			final TextView lastSleepTitleText = (TextView) findViewById(R.id.home_last_sleep_title_text);
			final TextView reviewTitleText = (TextView) findViewById(R.id.home_review_title_text);
			final RelativeLayout container = (RelativeLayout) findViewById(R.id.home_stats_container);
			if (cursor == null) {
				container.setVisibility(View.GONE);
				sleepChart.setVisibility(View.GONE);
				reviewTitleText
						.setText(getString(R.string.home_review_title_text_empty));
				lastSleepTitleText
						.setText(getString(R.string.home_last_sleep_title_text_empty));
			} else {

				final TextView avgScoreText = (TextView) findViewById(R.id.value_score_text);
				final TextView avgDurationText = (TextView) findViewById(R.id.value_duration_text);
				final TextView avgSpikesText = (TextView) findViewById(R.id.value_spikes_text);
				final TextView avgFellAsleepText = (TextView) findViewById(R.id.value_fell_asleep_text);
				cursor.moveToLast();
				sleepChart.setVisibility(View.VISIBLE);
				container.setVisibility(View.VISIBLE);
				try {
					sleepChart.sync(cursor);
				} catch (final StreamCorruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (final IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (final IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (final ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				lastSleepTitleText
						.setText(getString(R.string.home_last_sleep_title_text));

				cursor.moveToFirst();
				int avgSleepScore = 0;
				int avgDuration = 0;
				int avgSpikes = 0;
				int avgFellAsleep = 0;
				int count = 0;
				do {
					count++;
					final SleepRecord sleepRecord = new SleepRecord(cursor);
					avgSleepScore += sleepRecord.getSleepScore();
					avgDuration += sleepRecord.duration;
					avgSpikes += sleepRecord.spikes;
					avgFellAsleep += sleepRecord.getTimeToFallAsleep();
				} while (cursor.moveToNext());

				avgSleepScore /= count;
				avgDuration /= count;
				avgSpikes /= count;
				avgFellAsleep /= count;

				avgScoreText.setText(avgSleepScore + "%");
				avgDurationText.setText(SleepRecord.getTimespanText(
						avgDuration, getResources()));
				avgSpikesText.setText(avgSpikes + "");
				avgFellAsleepText.setText(SleepRecord.getTimespanText(
						avgFellAsleep, getResources()));

				reviewTitleText
						.setText(getString(R.string.home_review_title_text));
			}
		}

		@Override
		protected void onPreExecute() {
			sleepChart = (SleepChart) findViewById(R.id.home_sleep_chart);
		}

	}

	private SleepChart sleepChart;

	LoadLastSleepChartTask loadLastSleepChartTask;

	@Override
	protected int getContentAreaLayoutId() {
		return R.layout.activity_home;
	}

	public void onAlarmsClick(final View v) {
		startActivity(new Intent(this, AlarmClock.class));
	}

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		// do this before the google analytics tracker can send any data
		PreferenceManager.setDefaultValues(this, R.xml.settings, false);

		super.onCreate(savedInstanceState);

		// showTitleButton1(R.drawable.ic_title_share);
		// showTitleButton2(R.drawable.ic_title_refresh);
		setHomeButtonAsLogo();

		final SharedPreferences userPrefs = getSharedPreferences(
				SettingsActivity.PREFERENCES_ENVIRONMENT, Context.MODE_PRIVATE);

		final int prefsVersion = userPrefs.getInt(
				SettingsActivity.PREFERENCES_ENVIRONMENT, 0);
		if (prefsVersion == 0) {
			startActivity(new Intent(this, WelcomeTutorialWizardActivity.class)
					.putExtra("required", true));
		}else{
			WelcomeTutorialWizardActivity.enforceCalibrationBeforeStartingSleep(this); }
	}

	public void onHistoryClick(final View v) {
		startActivity(new Intent(this, MonthActivity.class));
	}

	@Override
	public void onHomeClick(final View v) {
		// do nothing b/c home is home!
	}

	@Override
	protected void onPause() {
		super.onPause();

		if (loadLastSleepChartTask != null) {
			loadLastSleepChartTask.cancel(true);
		}
	}

	protected void onRestart() {
		super.onRestart();

		final SharedPreferences userPrefs = getSharedPreferences(
				SettingsActivity.PREFERENCES_ENVIRONMENT, Context.MODE_PRIVATE);

		final boolean betaEndingShown = userPrefs.getBoolean("betaEndingShown",
				false);
		if (!betaEndingShown) {
			userPrefs.edit().putBoolean("betaEndingShown", true).commit();
			final AlertDialog.Builder dialog = new AlertDialog.Builder(this)
					.setTitle(R.string.beta_has_ended)
					.setMessage(R.string.beta_has_ended_message)
					.setPositiveButton(getString(R.string.donate),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(
										final DialogInterface dialog,
										final int id) {
									analytics.trackPageView("donate");
									final Uri marketUri = Uri
											.parse("market://details?id=com.androsz.electricsleepdonate");
									final Intent marketIntent = new Intent(
											Intent.ACTION_VIEW, marketUri);
									startActivity(marketIntent);

									// do a friendly little (invisible
									// :P) backup for the potential
									// donator
									File betaDb = HomeActivity.this
											.getDatabasePath(SleepHistoryDatabase.DATABASE_NAME);

									if (betaDb.exists()) {
										if (Environment.MEDIA_MOUNTED.equals(Environment
												.getExternalStorageState())) {
											File externalDb = new File(
													Environment
															.getExternalStorageDirectory()
															.getAbsolutePath(),
													SleepHistoryDatabase.DATABASE_NAME);

											try {
												DeviceUtil.copyFile(betaDb,
														externalDb);
											} catch (IOException e) {
												e.printStackTrace();
											}
										}
									}
								}
							})
					.setNeutralButton(getString(R.string.manual),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(
										final DialogInterface dialog,
										final int id) {
									analytics.trackPageView("manualInstall");
									startActivity(new Intent(
											"android.intent.action.VIEW",
											Uri.parse("http://code.google.com/p/electricsleep/downloads/list")));
								}
							})
					.setNegativeButton(getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(
										final DialogInterface dialog,
										final int id) {
									dialog.cancel();
								}
							});
			dialog.show();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (loadLastSleepChartTask != null) {
			loadLastSleepChartTask.cancel(true);
		}
		loadLastSleepChartTask = new LoadLastSleepChartTask();
		loadLastSleepChartTask.execute(getString(R.string.to));
	}

	public void onSleepClick(final View v) throws Exception {
		sendBroadcast(new Intent(StartSleepReceiver.START_SLEEP));
	}
}
